package com.gdziarmaga.webshop.web.menus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;

import org.richfaces.component.UIExtendedDataTable;

import com.gdziarmaga.webshop.book.model.Book;
import com.gdziarmaga.webshop.book.services.BookManager;
import com.gdziarmaga.webshop.client.model.Client;
import com.gdziarmaga.webshop.client.services.ClientManager;
import com.gdziarmaga.webshop.transaction.model.Transaction;
import com.gdziarmaga.webshop.transaction.services.TransactionManager;
import com.gdziarmaga.webshop.utils.filtering.BaseFilter;
import com.gdziarmaga.webshop.utils.filtering.FilterComparisonType;
import com.gdziarmaga.webshop.utils.filtering.FilterField;
import com.gdziarmaga.webshop.utils.springcontext.AppContext;
import com.gdziarmaga.webshop.web.utils.FacesUtils;

@ManagedBean(name = "adminPanelBean")
@SessionScoped
public class AdminPanelBean implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -7211940700262254607L;
	private List<Book> book_list;
	private List<Client> client_list;
	private List<Transaction> transaction_list;
	private List<Object> selectionItems = new ArrayList<Object>();
	private Collection<Object> selection;

	@PostConstruct
	public void init() {
		bookManager = (BookManager) AppContext.getApplicationContext().getBean(
				"bookManager");
		clientManager = (ClientManager) AppContext.getApplicationContext()
				.getBean("clientManager");
		transactionManager = (TransactionManager) AppContext
				.getApplicationContext().getBean("transManager");
	}

	public void selectionBookListener(AjaxBehaviorEvent event) {
		UIExtendedDataTable dataTable = (UIExtendedDataTable) event
				.getComponent();
		Object originalKey = dataTable.getRowKey();
		selectionItems.clear();
		for (Object selectionKey : selection) {
			dataTable.setRowKey(selectionKey);
			if (dataTable.isRowAvailable()) {
				selectionItems.add(dataTable.getRowData());
			}
		}
		dataTable.setRowKey(originalKey);
	}

	public void selectionTransactionListener(AjaxBehaviorEvent event) {
		UIExtendedDataTable dataTable = (UIExtendedDataTable) event
				.getComponent();
		Object originalKey = dataTable.getRowKey();
		selectionItems.clear();
		for (Object selectionKey : selection) {
			dataTable.setRowKey(selectionKey);
			if (dataTable.isRowAvailable()) {
				selectionItems.add(dataTable.getRowData());
			}
		}
		dataTable.setRowKey(originalKey);
	}

	public void selectionUserListener(AjaxBehaviorEvent event) {
		UIExtendedDataTable dataTable = (UIExtendedDataTable) event
				.getComponent();
		Object originalKey = dataTable.getRowKey();
		selectionItems.clear();
		for (Object selectionKey : selection) {
			dataTable.setRowKey(selectionKey);
			if (dataTable.isRowAvailable()) {
				selectionItems.add(dataTable.getRowData());
			}
		}
		dataTable.setRowKey(originalKey);
	}

	public List<Object> getSelectionItems() {
		return selectionItems;
	}

	public void setSelectionItems(List<Object> selectionItems) {
		this.selectionItems = selectionItems;
	}

	public Collection<Object> getSelection() {
		return selection;
	}

	public void setSelection(Collection<Object> selection) {
		this.selection = selection;
	}

	public List<Book> getBook_list() {
		if (book_list == null) {
			book_list = bookManager.getAll();

		}
		return book_list;
	}

	public void setBook_list(List<Book> book_list) {
		this.book_list = book_list;
	}

	public List<Client> getClient_list() {
		if (client_list == null) {
			BaseFilter filter = new BaseFilter();
			filter.addFilterField(new FilterField<Integer>(new Integer(1),
					"flag", FilterComparisonType.EQUAL));
			client_list = clientManager.getClientByFilter(filter);
			Set<Client> client_set = new HashSet<Client>(client_list);
			client_list = new ArrayList<Client>(client_set);
		}
		return client_list;
	}

	public List<Transaction> getTransaction_list() {
		if (transaction_list == null) {
			BaseFilter filter = new BaseFilter();
			filter.addFilterField(new FilterField<Integer>(new Integer(0),
					"realised", FilterComparisonType.EQUAL));
			transaction_list = transactionManager
					.getTransactionByFilter(filter);
			Set<Transaction> transaction_set = new HashSet<Transaction>(
					transaction_list);
			transaction_list = new ArrayList<Transaction>(transaction_set);
		}
		return transaction_list;
	}

	public void setClient_list(List<Client> client_list) {
		this.client_list = client_list;
	}

	public ClientManager getClientManager() {
		return clientManager;
	}

	public void setClientManager(ClientManager clientManager) {
		this.clientManager = clientManager;
	}

	public BookManager getBookManager() {
		return bookManager;
	}

	public void setBookManager(BookManager bookManager) {
		this.bookManager = bookManager;
	}

	public void changeSection(ActionEvent event) {
		// Map<String, String> params = FacesContext.getCurrentInstance()
		// .getExternalContext().getRequestParameterMap();
		// String selectedSection = params.get("adminSection");
		// FacesUtils.flashScope().put("adminSection", selectedSection);
	}

	public void refreshUserList() {
		client_list = null;
		getClient_list();
	}

	public void refreshTransactionList() {
		transaction_list = null;
		getTransaction_list();
	}

	public void refreshBookList() {
		book_list = null;
		getBook_list();
	}

	private ClientManager clientManager;
	private BookManager bookManager;
	private TransactionManager transactionManager;

}
